package schedule;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Random;

import model.Assessor;
import model.ProjectInfo;
import model.User;

import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.springframework.scheduling.quartz.QuartzJobBean;

import service.AssessorManager;
import service.ProjectInfoManager;
import service.UnAssessManager;

public class SearchUnassess extends QuartzJobBean {
	private int proInState;
	private ProjectInfoManager mgp;
	private AssessorManager mga;
	private UnAssessManager mgu;
	public void setMgu(UnAssessManager mgu) {
		this.mgu = mgu;
	}
	public void setMgp(ProjectInfoManager mgp) {
		this.mgp = mgp;
	}
	public void setMga(AssessorManager mga) {
		this.mga = mga;
	}
	@Override
	protected void executeInternal(JobExecutionContext arg0)
			throws JobExecutionException {
		// TODO Auto-generated method stub
		try {
			proInState = -1;
			System.out.println("备份程序运行.....");
			List<ProjectInfo> pros = mgp.getProjectByState(proInState);
			System.out.println(pros);
			if(pros != null){
				for(int i = 0; i<pros.size();i++){
					User user = pros.get(i).getUser();					
					int proInMain = pros.get(i).getProInMain();					
					List<Assessor> assessors = mga.getAssessorByTech(proInMain);
					//从assessors中随机选择3个
				    Random ran = new Random();
				    int count =3,j = 0;
				    List<Assessor> list = new ArrayList<Assessor>();
				    while(j < count) {
				    	Assessor tmp = assessors.get(ran.nextInt(assessors.size()));
				        if(list.contains(tmp)){
				            continue;
				        }else{
				            list.add(tmp);
				            j++;
				        }
				    }
				    for(int k = 0;k<list.size();k++){
				    	Date date = new Date();
				    	mgu.addUnAssess(date, user, list.get(k));
				    	//添加完后，将项目的状态设置成“评审”状态
				    	pros.get(i).setProInState(0);
				    	mgp.updateState(pros.get(i));
				    }
				 }
			}
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
			System.out.println("查找未评审创业项目信息时出现异常");
		}

	}

}
